
\documentclass{beamer}


\mode<presentation>
{
  \usetheme{CambridgeUS}
	\usecolortheme{beaver}
  % or ...

  \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}


\usepackage{xeCJK}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
\usepackage{pifont}
\usepackage{bibentry}
\usepackage{verbatim}
\usepackage{color}
\newcommand{\cmark}{\ding{51}}%
\newcommand{\xmark}{\ding{55}}%
\setCJKmainfont{WenQuanYi Micro Hei}
\renewcommand{\raggedright}{\leftskip=0pt \rightskip=0pt plus 0cm}
\raggedright

\let\oldfootnotesize\footnotesize
\renewcommand*{\footnotesize}{\oldfootnotesize\tiny}
\newcommand{\myfootnote}[1]{
    \renewcommand{\thefootnote}{}
    \footnotetext{\scriptsize#1}
    \renewcommand{\thefootnote}{\arabic{footnote}}
}

\title[MongoDB讲义] 
{MongoDB讲义}
\subtitle{MongoDB索引与导入}

\author[Zhilei Ren] 
{Zhilei Ren}

\institute[OSCAR Team] % (optional, but mostly needed)
{
\\\includegraphics[width=0.2\textwidth]{../utils/logo.jpg}\\
OSCAR Team, School of Software, 
Dalian University of Technology
}


\subject{MongoDB}



\pgfdeclareimage[height=0.5cm]{university-logo}{../utils/logo.jpg}
\logo{\pgfuseimage{university-logo}}



% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:
\AtBeginSubsection[]
{
  \begin{frame}<beamer>{Outline}
    \tableofcontents[currentsection,currentsubsection]
  \end{frame}
}


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command: 

%\beamerdefaultoverlayspecification{<+->}

\setbeamertemplate{section in toc}[circle]
\setbeamertemplate{items}[circle]
\setbeamertemplate{caption}[numbered]
\setbeamertemplate{bibliography item}{\insertbiblabel}
\setbeamertemplate{bibliography entry title}{}
\setbeamertemplate{bibliography entry journal}{}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

%\begin{frame}{Outline}
%  \tableofcontents[currentsection,currentsubsection, 
%    hideothersubsections, 
%    sectionstyle=show,
%]
%\end{frame}

\AtBeginSection[]
{
 \begin{frame}<beamer>
 \frametitle{Outline}
 \tableofcontents[currentsection]
 \end{frame}
}
\section{git mergetool} % (fold)
\label{sec:git mergetool}
\begin{frame}[t]{git冲突}
    \includegraphics[width=.4\textwidth]{git.png}
\end{frame}
\begin{frame}[t]{git mergetool}
    \begin{block}{git comands}
        \begin{itemize}
        \scriptsize
            \item git config merge.tool vimdiff (other tools exist too, e.g., meld, kdiff3)
            \item git mergetool (when conflict occurs)
            \item :diffget {1,2,3} (to choose contents in vimdiff)
            \item git commit 
        \end{itemize}
    \end{block}
    \centering
    \includegraphics[width=.6\textwidth]{mergetool.png}
\end{frame}
% section git mergetool (end)
\section{MongoDB Index}
\label{sec:MongoDB Index}
\begin{frame}[t,fragile]{准备数据}
	\begin{block}{Insert Data}
    \scriptsize
\begin{verbatim}
for (var i = 0; i < 1000000; i++) {
    db.users.insert(
    {
        "i" : i,
        "username" : "user" + i,
        "age" : Math.floor(Math.random() * 120),
        "created" : new Date()
    }
    );
};

\end{verbatim}
	\end{block}
\end{frame}

\begin{frame}[t,fragile]{无索引查询}
	\begin{block}{Retrieve without Index}
    \scriptsize
\begin{verbatim}
//no index
db.users.find({username: "user101"}).explain()
db.users.find({username: "user101"}).limit(1).explain()
db.users.find({username: "user99999"}).explain()

\end{verbatim}
	\end{block}
\end{frame}

\begin{frame}[t,fragile]{设定索引}
	\begin{block}{ensureIndex}
    \scriptsize
\begin{verbatim}
//index on username field
db.users.ensureIndex({username: 1})
db.users.find({username: "user101"}).explain()
db.users.find({username: "user101"}).limit(1).explain()
db.users.find({username: "user99999"}).explain()

db.users.find({age: {$gt: 60}}).explain()
\end{verbatim}
	\end{block}
\end{frame}

\begin{frame}[t,fragile]{复合索引与嵌套索引}
	\begin{block}{ensureIndex}
    \scriptsize
\begin{verbatim}
//compound
db.users.ensureIndex({age: 1, username: 1})
db.users.find({age:{$gt:60}}).explain()
//nested
db.another.insert({username:"sid",
                   loc:{ip: "1.2.3.4",
                        city: "Dalian",
                        province: "Liaoning"}
                  });
db.another.ensureIndex({"loc":1})
db.another.find({"loc.city":"Dalian"}).explain()
db.another.ensureIndex({"loc.city":1})
db.another.find({"loc.city":"Dalian"}).explain()
\end{verbatim}
	\end{block}
\end{frame}

\section{MongoDB Import}
\label{sec:MongoDB Import}

\begin{frame}[t]{数据导入}
	\begin{block}{功能简介}

		The mongoimport tool imports content from an Extended JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool.
	\end{block}
\end{frame}
\begin{frame}[t]{mongoimport参数}
	\tiny
	\begin{tabular}{ll}
-h, -{}-host=<hostname>                    &       mongodb host to connect to (setname/host1,host2 for replica sets)\\
-{}-port=<port>                            &   server port (can also use -{}-host hostname:port)\\
-d, -{}-db=<database-name>                 &       database to use\\
-c, -{}-collection=<collection-name>       &       collection to use\\
-f, -{}-fields=<field>[,<field>]*          &       comma separated list of field names, e.g.  -f name,age\\
-{}-file=<filename>                        &   file to import from; if not specified, stdin is used\\
-{}-headerline                             &   use first line in input source as the field list (CSV and TSV only)\\
-{}-type=<type>                            &   input format to import: json, csv, or tsv (defaults to 'json')\\
	\end{tabular}
	
\end{frame}

\begin{frame}[t]{Real-world Data Set}
	\begin{block}{Data from World Bank}
     

世界银行（英语：World Bank）是为发展中国家资本项目提供贷款的联合国系统国际金融机构。它是世界银行集团的组成机构之一，同时也是联合国发展集团（UNDP）的成员。

世界银行的官方目标为消除贫困。根据其有关协定规定（修订并于1989年2月16日生效），其所有决定都必须旨在推动外商直接投资和国际贸易，以及为资本投资提供便利。
\footnote{\url{https://zh.wikipedia.org/zh-cn/世界银行}}

	\end{block}
\end{frame}

\begin{frame}[t]{世界银行}
    \begin{block}{今天就能开始用的小知识}
        \begin{itemize}
					\item 现任世行行长为金墉\footnote{\url{https://zh.wikipedia.org/wiki/金墉}}
					\item 不是那个金庸\footnote{\url{https://zh.wikipedia.org/wiki/金庸}}
        \end{itemize}
    \end{block}
\end{frame}

\begin{frame}[t]{Data Set}
	\begin{itemize}
		\item DataBank is an analysis and visualisation tool that contains collections of time series data on a variety of topics.
		\item \url{https://data.worldbank.org/country}
		\item \url{http://databank.worldbank.org}
	\end{itemize}
	\begin{minipage}{.3\textwidth}
		\includegraphics[width=\textwidth]{front.png}
	\end{minipage}
	\begin{minipage}{.3\textwidth}
		\includegraphics[width=\textwidth]{list.png}
	\end{minipage}
	\begin{minipage}{.3\textwidth}
		\includegraphics[width=\textwidth]{page.png}
	\end{minipage}
\end{frame}

\begin{frame}[t]{数据样例}
	\includegraphics[width=.9\textwidth]{sample.png}
\end{frame}

\begin{frame}[t,fragile]{导入脚本}
\begin{itemize}
	\item ../data/import.sh
	\item ../data/import.py works as well
	\item 需要首先启动mongod 
\end{itemize}

\tiny
\begin{verbatim}
#!/bin/bash

if [[ $(ls *.csv|wc -l) > 0 ]]; then
    rm *.csv
fi

for file in *.zip
do
    unzip $file
done

for file in API*.csv
do
    sed -i '1,4d' $file
    mongoimport --db tut --collection worldbank --type csv --headerline $file
done
\end{verbatim}
\end{frame}

\end{document}
